<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="javax.sql.*,java.sql.*, java.io.PrintWriter" %>
    
    <%
	//检查会话中是否有 "wy" 属性
	Object fdyObj = session.getAttribute("wy");
	if (fdyObj == null) {
	    response.sendRedirect("Login.jsp");
	    return;
	}
%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>物业页面</title>
<link href="../css/Utils_wx.css" rel="stylesheet" type="text/css" />
<!-- 退出登录专用 -->
   <script type="text/javascript" src="../view/Loginout.js"></script>
</head>
<body>

<!-- 头部 -->
<div class="header" ><br>
<nerong>&nbsp;&nbsp;物业页面</nerong>

<div class="bb" >	
<br>

 <button onclick="confirmLogout('<%= (String)session.getAttribute("wy") %>','wy')">退出登录</button>

</div>
</div><br>

<div class="left" >
<table width="80%" height="80%">
<th>
<nerong>物业可选择内容</nerong>

</th>
<tr>
<td >
	<a href="../View_Msg/msgWy.jsp">&emsp;个人信息</a><br>
</td>
</tr>

<tr>
<td bgcolor=#1a7dba>
<a href="WyFix.jsp">&emsp;报修信息处理</a><br>
</td>
</tr>

<tr>
<td>
<a href = "../View_SuShe/SuSheWyMain.jsp">&emsp;宿舍硬件信息管理</a><br>
</td>
</tr>


</table>

</div>



<div class="context" >
<!-- 这部分是每个大功能的小功能 -->
<div class="fix" style="height:auto;" >
<div>
<!-- 这个模块可以筛选已经处理的未处理的，查询根据楼号和楼层房号等查询相应数据 -->
<form action="wyFixSelect.jsp" method="post">
    <label>楼号</label>
    <input type="text" name="building">
    <label>楼层</label>
    <input type="text" name="floor">
    <input type="submit" value="查询">
	<input type="button" onclick="window.location.href='WyFix.jsp';" value="返回">
</form>


</div>
<table border="1" align="center" style="width:100%;">
    <tr>
        <th>报修单号</th>
        <th>楼号</th>
        <th>楼层</th>
        <th>房号</th>
        <th>时间</th>
        <th>细节</th>
        <th>类型</th>
        <th>解决状态</th>
        <th> </th>
    </tr>
    <%
        int pageSize = 5; // 每页显示的记录数
        int currentPage = 1; // 默认当前页码为1
        String pageParam = request.getParameter("page");
        if (pageParam != null && !pageParam.isEmpty()) {
            currentPage = Integer.parseInt(pageParam);
        }
        
        String building = request.getParameter("building");
        String floor = request.getParameter("floor");
        
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        ResultSet rsTotal = null;
        int totalRecords = 0;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaweb?serverTimezone=UTC", "root", "aa5678989");
            
            // 计算总记录数（考虑筛选条件）
            String totalQuery = "SELECT COUNT(*) FROM fix WHERE (Fix_Buliding LIKE CONCAT('%', ?, '%') AND Fix_floor LIKE CONCAT('%', ?, '%'))";
            PreparedStatement pstTotal = con.prepareStatement(totalQuery);
            pstTotal.setString(1, building);
            pstTotal.setString(2, floor);
            rsTotal = pstTotal.executeQuery();
            if (rsTotal.next()) {
                totalRecords = rsTotal.getInt(1);
            }
            rsTotal.close();
            pstTotal.close();
            
            int totalPages = (int) Math.ceil((double) totalRecords / pageSize);
            
            // 查询当前页的数据（考虑筛选条件）
            int offset = (currentPage - 1) * pageSize;
            String query = "SELECT * FROM fix WHERE (Fix_Buliding LIKE CONCAT('%', ?, '%') AND Fix_floor LIKE CONCAT('%', ?, '%')) LIMIT ? OFFSET ?";
            pst = con.prepareStatement(query);
            pst.setString(1, building);
            pst.setString(2, floor);
            pst.setInt(3, pageSize);
            pst.setInt(4, offset);
            rs = pst.executeQuery();
            
            while (rs.next()) {
                String id = rs.getString("Fix_id");
                String buildingValue = rs.getString("Fix_Buliding");
                String floorValue = rs.getString("Fix_floor");
                String room = rs.getString("Fix_BuilNum"); // 假设这里的字段名已经修正
                String time = rs.getString("Fix_Time");
                String detail = rs.getString("Fix_detail");
                String type = rs.getString("Fix_type");
                String fixed=rs.getString("Fixed");
    %>
    <tr>
        <td><%= id %></td>
        <td><%= buildingValue %></td>
        <td><%= floorValue %></td>
        <td><%= room %></td>
        <td><%= time %></td>
        <td><%= detail %></td>
        <td><%= (type != null && fixed.equals("1")) ? "宿舍报修" : "公物报修" %></td>
         <td><%= (fixed != null && fixed.equals("1")) ? "已解决" : "未解决" %></td>
         <td>
         <form action="updateFixStatus.jsp" method="post" style="display:inline;">
            <input type="hidden" name="fixId" value="<%= id %>">
            <input type="hidden" name="action" value="resolve">
            <input type="submit" value="已解决">
        </form>
         </td>
        
    </tr>
    <%
            }
        } catch (Exception e) {
            e.printStackTrace(new PrintWriter(out));
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(new PrintWriter(out)); }
            try { if (pst != null) pst.close(); } catch (SQLException e) { e.printStackTrace(new PrintWriter(out)); }
            try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(new PrintWriter(out)); }
        }
    %>
</table>
<!-- 分页链接 -->

<div style="background-color:#a5d4f1;">
<%
    for (int i = 1; i <= (int) Math.ceil((double) totalRecords / pageSize); i++) {
%>
     <a  href="wyFixSelect.jsp?page=<%= i %>">[<%= i %>]页</a>
<%
    }
%>
</div>
</div>


</div>

</body>
</html>